package com.moshang.exportsql.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.moshang.exportsql.model.MetaClassAttributeVo;
import com.moshang.exportsql.model.MetaClassInfo;
import com.moshang.exportsql.model.enums.AttributeInfoCategoryEnum;
import com.moshang.exportsql.util.NancalStringUtils;
import com.moshang.exportsql.util.RedisCacheNames;
import com.moshang.exportsql.util.RedisCacheUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Slf4j
public class ExportSqlController {
    private  final NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @GetMapping("/export0329")
    public void export0329() throws JsonProcessingException {
        String tenantId="0";
        // 执行SQL查询
        String keyPrefix= RedisCacheNames.getMetaClassPrefix() + tenantId +":";
        String result = RedisCacheUtils.get(keyPrefix+"MlService");
        List<Map<String, Object>> combinedQueryResult = new ArrayList<>();
        if (StrUtil.isNotBlank(result)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(result, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs =metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds=new ArrayList<>();
            permanentAttrs.forEach(item->{
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql="SELECT "+StrUtil.join(",",fileds)+" FROM ml_service WHERE (container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') or " +
                    "code='bizsvr') and tenant_id='0'";
            List<Map<String, Object>> mlServiceQueryResult = namedParameterJdbcTemplate.queryForList(sql, new MapSqlParameterSource());
            mlServiceQueryResult.forEach(map -> map.put("itable_name", "ml_service"));
            combinedQueryResult.addAll(mlServiceQueryResult);
        }
        String mlClassResult = RedisCacheUtils.get(keyPrefix+"MlClass");
        if (StrUtil.isNotBlank(mlClassResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlClassResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs =metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds=new ArrayList<>();
            permanentAttrs.forEach(item->{
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql="SELECT "+StrUtil.join(",",fileds)+" FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'";
            List<Map<String, Object>> mlClassQueryResult = namedParameterJdbcTemplate.queryForList(sql, new MapSqlParameterSource());
            mlClassQueryResult.forEach(map -> map.put("itable_name", "ml_class"));
            combinedQueryResult.addAll(mlClassQueryResult);
        }
        String clsAttrResult = RedisCacheUtils.get(keyPrefix+"ClsAttr");
        if (StrUtil.isNotBlank(clsAttrResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(clsAttrResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs =metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds=new ArrayList<>();
            permanentAttrs.forEach(item->{
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql="SELECT "+StrUtil.join(",",fileds)+" FROM cls_attr WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'";

            List<Map<String, Object>> clsAttrQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            clsAttrQueryResult.forEach(map -> map.put("itable_name", "cls_attr"));
            combinedQueryResult.addAll(clsAttrQueryResult);
        }
        String clsMsgResult = RedisCacheUtils.get(keyPrefix+"ClsMsg");
        if (StrUtil.isNotBlank(clsMsgResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(clsMsgResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs =metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds=new ArrayList<>();
            permanentAttrs.forEach(item->{
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql="SELECT "+StrUtil.join(",",fileds)+" FROM cls_msg WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'";

            List<Map<String, Object>> clsMsgQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            clsMsgQueryResult.forEach(map -> map.put("itable_name", "cls_msg"));
            combinedQueryResult.addAll(clsMsgQueryResult);
        }
        String ClsConstResult = RedisCacheUtils.get(keyPrefix+"ClsConst");
        if (StrUtil.isNotBlank(ClsConstResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(ClsConstResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs =metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds=new ArrayList<>();
            permanentAttrs.forEach(item->{
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql="SELECT "+StrUtil.join(",",fileds)+" FROM cls_const WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');";

            List<Map<String, Object>> clsConstQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            clsConstQueryResult.forEach(map -> map.put("itable_name", "cls_const"));
            combinedQueryResult.addAll(clsConstQueryResult);
        }
        String MlAttributeResult = RedisCacheUtils.get(keyPrefix+"MlAttribute");
        if (StrUtil.isNotBlank(MlAttributeResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(MlAttributeResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs =metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds=new ArrayList<>();
            permanentAttrs.forEach(item->{
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql="SELECT "+StrUtil.join(",",fileds)+" FROM ml_attribute WHERE obj_id IN(SELECT right_id FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> mlAttributeQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlAttributeQueryResult.forEach(map -> map.put("itable_name", "ml_attribute"));
            combinedQueryResult.addAll(mlAttributeQueryResult);
        }
        String mlMessageResult = RedisCacheUtils.get(keyPrefix+"MlMessage");
        if (StrUtil.isNotBlank(mlMessageResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlMessageResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs =metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds=new ArrayList<>();
            permanentAttrs.forEach(item->{
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql="SELECT "+StrUtil.join(",",fileds)+" FROM ml_message WHERE obj_id IN(SELECT right_id FROM cls_msg WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> mlMessageQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlMessageQueryResult.forEach(map -> map.put("itable_name", "ml_message"));
            combinedQueryResult.addAll(mlMessageQueryResult);
        }
        String mlConstantResult = RedisCacheUtils.get(keyPrefix+"MlConstant");
        if (StrUtil.isNotBlank(mlConstantResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlConstantResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM ml_constant WHERE obj_id IN(SELECT right_id FROM cls_const WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> mlConstantQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlConstantQueryResult.forEach(map -> map.put("itable_name", "ml_constant"));
            combinedQueryResult.addAll(mlConstantQueryResult);
        }
        String mlRuleResult = RedisCacheUtils.get(keyPrefix+"MlRule");
        if (StrUtil.isNotBlank(mlRuleResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlRuleResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM ml_rule WHERE code IN(SELECT rule_code FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0') and rule_code !='' and rule_code is not null);";

            List<Map<String, Object>> mlRuleQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlRuleQueryResult.forEach(map -> map.put("itable_name", "ml_rule"));
            combinedQueryResult.addAll(mlRuleQueryResult);
        }
        // 智能体厂
        // List<Map<String, Object>> mlDbInfoQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM " +
        //          "ml_db_info WHERE obj_id IN(SELECT right_id FROM svr_db WHERE left_id IN(SELECT obj_id FROM " +
        //           "ml_service WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));",
        //        new MapSqlParameterSource());
        // List<Map<String, Object>> svrDbQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM svr_db " +
        //        "WHERE left_id IN(SELECT obj_id FROM ml_service WHERE (container in('LeVault','BPM','EntArch',
        //        'DME') or code='bizsvr') and tenant_id='0');", new MapSqlParameterSource());
        String mlLangResult = RedisCacheUtils.get(keyPrefix+"MlLang");
        if (StrUtil.isNotBlank(mlLangResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlLangResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM ml_lang WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0';";

            List<Map<String, Object>> mlLangQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlLangQueryResult.forEach(map -> map.put("itable_name", "ml_lang"));
            combinedQueryResult.addAll(mlLangQueryResult);
        }
//        List<Map<String, Object>> mlCodeRepoQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM ml_code_repo", new MapSqlParameterSource());
        String attrLovResult = RedisCacheUtils.get(keyPrefix+"AttrLov");
        if (StrUtil.isNotBlank(attrLovResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(attrLovResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM attr_lov WHERE left_id in(SELECT obj_id FROM ml_attribute WHERE obj_id IN(SELECT right_id FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0')));";

            List<Map<String, Object>> attrLovQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            attrLovQueryResult.forEach(map -> map.put("itable_name", "attr_lov"));
            combinedQueryResult.addAll(attrLovQueryResult);
        }
        String attrCodeResult = RedisCacheUtils.get(keyPrefix+"AttrCode");
        if (StrUtil.isNotBlank(attrCodeResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(attrCodeResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM attr_code WHERE left_id in(SELECT obj_id FROM" +
                    " ml_attribute WHERE obj_id IN(SELECT right_id FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0')));";

            List<Map<String, Object>> attrCodeQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            attrCodeQueryResult.forEach(map -> map.put("itable_name", "attr_code"));
            combinedQueryResult.addAll(attrCodeQueryResult);
        }
        String mlDialogLayoutResult = RedisCacheUtils.get(keyPrefix+"MlDialogLayout");
        if (StrUtil.isNotBlank(mlDialogLayoutResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlDialogLayoutResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM ml_dialog_layout where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0';";

            List<Map<String, Object>> mlDialogLayOutQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlDialogLayOutQueryResult.forEach(map -> map.put("itable_name", "ml_dialog_layout"));
            combinedQueryResult.addAll(mlDialogLayOutQueryResult);
        }
        String mlIndexResult = RedisCacheUtils.get(keyPrefix+"MlIndex");
        if (StrUtil.isNotBlank(mlIndexResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlIndexResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + "  FROM ml_index where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0';";

            List<Map<String, Object>> mlIndexQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlIndexQueryResult.forEach(map -> map.put("itable_name", "ml_index"));
            combinedQueryResult.addAll(mlIndexQueryResult);
        }
        String mlAttrRelResult = RedisCacheUtils.get(keyPrefix+"MlAttrRel");
        if (StrUtil.isNotBlank(mlAttrRelResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlAttrRelResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + "  FROM ml_attr_rel where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0';";

            List<Map<String, Object>> mlAttrRelQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlAttrRelQueryResult.forEach(map -> map.put("itable_name", "ml_attr_rel"));
            combinedQueryResult.addAll(mlAttrRelQueryResult);
        }
        String mlRuntimeClsResult = RedisCacheUtils.get(keyPrefix+"MlRuntimeCls");
        if (StrUtil.isNotBlank(mlRuntimeClsResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlRuntimeClsResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + "  FROM ml_runtime_relation_class where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0';";

            List<Map<String, Object>> mlRuntimeClassQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlRuntimeClassQueryResult.forEach(map -> map.put("itable_name", "ml_runtime_relation_class"));
            combinedQueryResult.addAll(mlRuntimeClassQueryResult);
        }
        List<Map<String, Object>> mlServiceIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_service WHERE (container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') or code='bizsvr') and tenant_id='0');", new MapSqlParameterSource());
        List<Map<String, Object>> mlClassIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');", new MapSqlParameterSource());
        List<Map<String, Object>> clsAttrIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM cls_attr WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');", new MapSqlParameterSource());
        List<Map<String, Object>> clsAttrRelationIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * " +
                "FROM " +
                "relation_id where obj_id in(SELECT obj_id FROM cls_attr WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');", new MapSqlParameterSource());

        List<Map<String, Object>> clsMsgIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM cls_msg WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');", new MapSqlParameterSource());
        List<Map<String, Object>> clsMsgRelationIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * " +
                "FROM " +
                "relation_id where obj_id in(SELECT obj_id FROM cls_msg WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');", new MapSqlParameterSource());

        List<Map<String, Object>> clsConstIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM cls_const WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));", new MapSqlParameterSource());
        List<Map<String, Object>> clsConstRelationIdIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM relation_id where obj_id in(SELECT obj_id FROM cls_const WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));", new MapSqlParameterSource());

        List<Map<String, Object>> mlAttributeIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_attribute WHERE obj_id IN(SELECT right_id FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0')));", new MapSqlParameterSource());
        List<Map<String, Object>> mlMessageIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_message WHERE obj_id IN(SELECT right_id FROM cls_msg WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0')));", new MapSqlParameterSource());
        List<Map<String, Object>> mlConstantIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_constant WHERE obj_id IN(SELECT right_id FROM cls_const WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0')));", new MapSqlParameterSource());
        List<Map<String, Object>> mlRuleIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_rule WHERE code IN(SELECT rule_code FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0') and rule_code !='' and rule_code is not null));", new MapSqlParameterSource());
        List<Map<String, Object>> mlDbInfoIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_db_info WHERE obj_id IN(SELECT right_id FROM svr_db WHERE left_id IN(SELECT obj_id FROM ml_service WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0')));", new MapSqlParameterSource());
        List<Map<String, Object>> svrDbIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM svr_db WHERE left_id IN(SELECT obj_id FROM ml_service WHERE (container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') or code='bizsvr') and tenant_id='0'));", new MapSqlParameterSource());
        List<Map<String, Object>> svrDbRelationIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM" +
                " relation_id where obj_id in(SELECT obj_id FROM svr_db WHERE left_id IN(SELECT obj_id FROM ml_service WHERE (container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') or code='bizsvr') and tenant_id='0'));", new MapSqlParameterSource());

        List<Map<String, Object>> mlLangIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_lang WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');", new MapSqlParameterSource());
//        List<Map<String, Object>> mlCodeRepoIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_code_repo);", new MapSqlParameterSource());
        List<Map<String, Object>>  attrLovIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM attr_lov WHERE left_id in(SELECT obj_id FROM ml_attribute WHERE obj_id IN(SELECT right_id FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'))));", new MapSqlParameterSource());
        List<Map<String, Object>>  attrCodeIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM attr_code WHERE left_id in(SELECT obj_id FROM ml_attribute WHERE obj_id IN(SELECT right_id FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'))));", new MapSqlParameterSource());
        List<Map<String, Object>>  mlDialogLayOutIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_dialog_layout where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');", new MapSqlParameterSource());
        List<Map<String, Object>>  mlIndexIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM " +
                "unique_id where obj_id in(SELECT obj_id FROM ml_index where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') " +
                "and tenant_id='0');", new MapSqlParameterSource());
        List<Map<String, Object>>  mlAttRelIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM " +
                "unique_id where obj_id in(SELECT obj_id FROM ml_attr_rel where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') " +
                "and tenant_id='0');", new MapSqlParameterSource());
        List<Map<String, Object>>  mlRuntimeClassIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * " +
                "FROM " +
                "unique_id where obj_id in(SELECT obj_id FROM ml_runtime_relation_class where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') " +
                "and tenant_id='0');", new MapSqlParameterSource());

        // 合并两个查询的结果

        //   mlDbInfoQueryResult.forEach(map -> map.put("itable_name", "ml_db_info"));
        //    svrDbQueryResult.forEach(map -> map.put("itable_name", "svr_db"));

        //  mlCodeRepoQueryResult.forEach(map -> map.put("itable_name", "ml_code_repo"));







        mlServiceIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlClassIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        clsAttrIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        clsMsgIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        clsConstIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlAttributeIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlMessageIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlConstantIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlRuleIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlDbInfoIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        svrDbIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));

        mlLangIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        // mlCodeRepoIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        attrLovIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        attrCodeIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlDialogLayOutIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlIndexIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlAttRelIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlRuntimeClassIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        svrDbRelationIdQueryResult.forEach(map -> map.put("itable_name", "relation_id"));
        clsAttrRelationIdQueryResult.forEach(map -> map.put("itable_name", "relation_id"));
        clsMsgRelationIdQueryResult.forEach(map -> map.put("itable_name", "relation_id"));
        clsConstRelationIdIdQueryResult.forEach(map -> map.put("itable_name", "relation_id"));
        // combinedQueryResult.addAll(mlDbInfoQueryResult);
        // combinedQueryResult.addAll(svrDbQueryResult);
        combinedQueryResult.addAll(mlServiceIdQueryResult);
        combinedQueryResult.addAll(mlClassIdQueryResult);
        combinedQueryResult.addAll(clsAttrIdQueryResult);
        combinedQueryResult.addAll(clsMsgIdQueryResult);
        combinedQueryResult.addAll(clsConstIdQueryResult);
        combinedQueryResult.addAll(mlAttributeIdQueryResult);
        combinedQueryResult.addAll(mlMessageIdQueryResult);
        combinedQueryResult.addAll(mlConstantIdQueryResult);
        combinedQueryResult.addAll(mlRuleIdQueryResult);
        //combinedQueryResult.addAll(mlDbInfoIdQueryResult);
        //combinedQueryResult.addAll(svrDbIdQueryResult);
        combinedQueryResult.addAll(mlLangIdQueryResult);
        // combinedQueryResult.addAll(mlCodeRepoIdQueryResult);
        combinedQueryResult.addAll(attrLovIdQueryResult);
        combinedQueryResult.addAll(attrCodeIdQueryResult);
        combinedQueryResult.addAll(mlDialogLayOutIdQueryResult);
        combinedQueryResult.addAll(mlIndexIdQueryResult);
        combinedQueryResult.addAll(mlAttRelIdQueryResult);
        combinedQueryResult.addAll(mlRuntimeClassIdQueryResult);
        combinedQueryResult.addAll(svrDbRelationIdQueryResult);
        combinedQueryResult.addAll(clsAttrRelationIdQueryResult);
        combinedQueryResult.addAll(clsMsgRelationIdQueryResult);
        combinedQueryResult.addAll(clsConstRelationIdIdQueryResult);
        // 生成动态列的SQL脚本并导出为文件
        exportSqlScript(combinedQueryResult, "2智能体厂","0329基线-智能体厂");

        //模型解析
        List<Map<String, Object>> metaCombinedQueryResult = new ArrayList<>();
        String metaServiceResult = RedisCacheUtils.get(keyPrefix+"MetaService");
        if (StrUtil.isNotBlank(metaServiceResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaServiceResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_service where code in (select code from ml_service where (container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') or code='bizsvr') and tenant_id='0');";

            List<Map<String, Object>> metaServiceQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaServiceQueryResult.forEach(map -> map.put("itable_name", "meta_service"));
            metaCombinedQueryResult.addAll(metaServiceQueryResult);
        }
        String metaClassResult = RedisCacheUtils.get(keyPrefix+"MetaClass");
        if (StrUtil.isNotBlank(metaClassResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaClassResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_class where service_code in (select code from meta_service where code in (select code from ml_service where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> metaClassQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaClassQueryResult.forEach(map -> map.put("itable_name", "meta_class"));
            metaCombinedQueryResult.addAll(metaClassQueryResult);
        }
        String metaClassConstantResult = RedisCacheUtils.get(keyPrefix+"MetaClassConstant");
        if (StrUtil.isNotBlank(metaClassConstantResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaClassConstantResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_class_constant where service_code in (select code from meta_service where code in (select code from ml_service where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> metaClassConstQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaClassConstQueryResult.forEach(map -> map.put("itable_name", "meta_class_constant"));
            metaCombinedQueryResult.addAll(metaClassConstQueryResult);
        }
        String metaClassAttributeResult = RedisCacheUtils.get(keyPrefix+"MetaClassAttribute");
        if (StrUtil.isNotBlank(metaClassAttributeResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaClassAttributeResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_class_attribute where service_code in (select code from meta_service where code in (select code from ml_service where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> metaClassAttributeQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaClassAttributeQueryResult.forEach(map -> map.put("itable_name", "meta_class_attribute"));
            metaCombinedQueryResult.addAll(metaClassAttributeQueryResult);
        }
        String metaClassMethodResult = RedisCacheUtils.get(keyPrefix+"MetaClassMethod");
        if (StrUtil.isNotBlank(metaClassMethodResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaClassMethodResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_class_method where service_code in (select code from meta_service where code in (select code from ml_service where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> metaClassMethodQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaClassMethodQueryResult.forEach(map -> map.put("itable_name", "meta_class_method"));
            metaCombinedQueryResult.addAll(metaClassMethodQueryResult);
        }
        List<Map<String, Object>> metaDataSourceQueryResult = namedParameterJdbcTemplate.queryForList("select * from meta_data_source where service_code in (select code from meta_service where code in (select code from ml_service where (container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread')or code='bizsvr') and tenant_id='0'));", new MapSqlParameterSource());
        String metaLangDataResult = RedisCacheUtils.get(keyPrefix+"MetaLangData");
        if (StrUtil.isNotBlank(metaLangDataResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaLangDataResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_lang_data where service_code in (select code from meta_service where code in (select code from ml_service where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> metaLangDataQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaLangDataQueryResult.forEach(map -> map.put("itable_name", "meta_lang_data"));
            metaCombinedQueryResult.addAll(metaLangDataQueryResult);
        }
        String metaDialogLayoutResult = RedisCacheUtils.get(keyPrefix+"MetaDialogLayout");
        if (StrUtil.isNotBlank(metaDialogLayoutResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaDialogLayoutResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_dialog_layout where service_code in (select code from meta_service where code in (select code from ml_service where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> metaDialogLayoutQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaDialogLayoutQueryResult.forEach(map -> map.put("itable_name", "meta_dialog_layout"));
            metaCombinedQueryResult.addAll(metaDialogLayoutQueryResult);
        }
        String metaAttrRelResult = RedisCacheUtils.get(keyPrefix+"MetaAttrRel");
        if (StrUtil.isNotBlank(metaAttrRelResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaAttrRelResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_attr_rel where tenant_id=0";

            List<Map<String, Object>> metaAttrRelQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaAttrRelQueryResult.forEach(map -> map.put("itable_name", "meta_attr_rel"));
            metaCombinedQueryResult.addAll(metaAttrRelQueryResult);
        }
        String metaRuntimeClsResult = RedisCacheUtils.get(keyPrefix+"MetaRuntimeCls");
        if (StrUtil.isNotBlank(metaRuntimeClsResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaRuntimeClsResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_runtime_class_relation where tenant_id=0";

            List<Map<String, Object>> metaRunTimeQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaRunTimeQueryResult.forEach(map -> map.put("itable_name", "meta_runtime_class_relation"));
            metaCombinedQueryResult.addAll(metaRunTimeQueryResult);
        }
        // metaDataSourceQueryResult.forEach(map -> map.put("itable_name", "meta_data_source"));

        //  metaCombinedQueryResult.addAll(metaDataSourceQueryResult);
        // 生成动态列的SQL脚本并导出为文件
        exportSqlScript(metaCombinedQueryResult, "3模型解析","0329基线-智能体厂");


        List<Map<String, Object>> lovCombinedQueryResult = new ArrayList<>();
        String lovDefResult = RedisCacheUtils.get(keyPrefix+"LovDef");
        if (StrUtil.isNotBlank(lovDefResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(lovDefResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM lov_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0';";

            List<Map<String, Object>> lovDefQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            lovDefQueryResult.forEach(map -> map.put("itable_name", "lov_def"));
            lovCombinedQueryResult.addAll(lovDefQueryResult);
        }
        String dynLovResult = RedisCacheUtils.get(keyPrefix+"DynLov");
        if (StrUtil.isNotBlank(dynLovResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(dynLovResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add("`" + NancalStringUtils.dumpToUnderline(item) + "`");
            });
            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM dyn_lov where lov_code in (SELECT code FROM lov_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread'));";

            List<Map<String, Object>> dynLovQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            dynLovQueryResult.forEach(map -> map.put("itable_name", "dyn_lov"));
            lovCombinedQueryResult.addAll(dynLovQueryResult);
        }
        String staticLovvResult = RedisCacheUtils.get(keyPrefix+"StaticLov");
        if (StrUtil.isNotBlank(staticLovvResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(staticLovvResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add("`" + NancalStringUtils.dumpToUnderline(item) + "`");
            });
            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM static_lov where `lov_code` in (SELECT code " +
                    "FROM lov_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread'));";

            List<Map<String, Object>> staticLovQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            staticLovQueryResult.forEach(map -> map.put("itable_name", "static_lov"));
            lovCombinedQueryResult.addAll(staticLovQueryResult);
        }
        List<Map<String, Object>> lovDefIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM lov_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread'));", new MapSqlParameterSource());
        List<Map<String, Object>> dynLovIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM dyn_lov where `lov_code` in (SELECT code FROM lov_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread')));", new MapSqlParameterSource());
        List<Map<String, Object>> staticLovIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM static_lov where `lov_code` in (SELECT code FROM lov_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread')));", new MapSqlParameterSource());

        lovDefIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        dynLovIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        staticLovIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        lovCombinedQueryResult.addAll(lovDefIdQueryResult);
        lovCombinedQueryResult.addAll(dynLovIdQueryResult);
        lovCombinedQueryResult.addAll(staticLovIdQueryResult);
        // 生成动态列的SQL脚本并导出为文件
        exportSqlScript(lovCombinedQueryResult, "4lov","0329基线-智能体厂");


        List<Map<String, Object>> encCodeCombinedQueryResult = new ArrayList<>();
        String codeDefResult = RedisCacheUtils.get(keyPrefix+"CodeDef");
        if (StrUtil.isNotBlank(codeDefResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(codeDefResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add("`" + NancalStringUtils.dumpToUnderline(item) + "`");
            });
            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM code_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread');";

            List<Map<String, Object>> codeDefQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            codeDefQueryResult.forEach(map -> map.put("itable_name", "code_def"));
            encCodeCombinedQueryResult.addAll(codeDefQueryResult);
        }

        String codeSegResult = RedisCacheUtils.get(keyPrefix+"CodeSeg");
        if (StrUtil.isNotBlank(codeSegResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(codeSegResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add("`" + NancalStringUtils.dumpToUnderline(item) + "`");
            });
            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM code_seg where `encode` in (SELECT code FROM code_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread'));";

            List<Map<String, Object>> codeSegQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            codeSegQueryResult.forEach(map -> map.put("itable_name", "code_seg"));
            encCodeCombinedQueryResult.addAll(codeSegQueryResult);
        }
        String codeValResult = RedisCacheUtils.get(keyPrefix+"CodeVal");
        if (StrUtil.isNotBlank(codeValResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(codeValResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add("`" + NancalStringUtils.dumpToUnderline(item) + "`");
            });
            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM code_val where `encode` in (SELECT code FROM code_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread'));";

            List<Map<String, Object>> codeValQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            codeValQueryResult.forEach(map -> map.put("itable_name", "code_val"));
            encCodeCombinedQueryResult.addAll(codeValQueryResult);
        }
        List<Map<String, Object>> codeDefIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM code_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread'));", new MapSqlParameterSource());
        List<Map<String, Object>> codeSegIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM code_seg where `encode` in (SELECT code FROM code_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread')));", new MapSqlParameterSource());
        List<Map<String, Object>> codeValIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM code_val where `encode` in (SELECT code FROM code_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread')));", new MapSqlParameterSource());






        codeDefIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        codeSegIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        codeValIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));

        encCodeCombinedQueryResult.addAll(codeDefIdQueryResult);
        encCodeCombinedQueryResult.addAll(codeSegIdQueryResult);
        encCodeCombinedQueryResult.addAll(codeValIdQueryResult);
        // 生成动态列的SQL脚本并导出为文件
        exportSqlScript(encCodeCombinedQueryResult, "5编码规则","0329基线-智能体厂");
    }
    @GetMapping("/exportsql")
    public void exportsql() throws JsonProcessingException {
        String tenantId="0";
        // 执行SQL查询
        String keyPrefix= RedisCacheNames.getMetaClassPrefix() + tenantId +":";
        String result = RedisCacheUtils.get(keyPrefix+"MlService");
        List<Map<String, Object>> combinedQueryResult = new ArrayList<>();
        if (StrUtil.isNotBlank(result)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(result, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs =metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds=new ArrayList<>();
            permanentAttrs.forEach(item->{
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql="SELECT "+StrUtil.join(",",fileds)+" FROM ml_service WHERE (container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') or " +
                    "code='bizsvr') and tenant_id='0'";
            List<Map<String, Object>> mlServiceQueryResult = namedParameterJdbcTemplate.queryForList(sql, new MapSqlParameterSource());
            mlServiceQueryResult.forEach(map -> map.put("itable_name", "ml_service"));
            combinedQueryResult.addAll(mlServiceQueryResult);
        }
        String mlClassResult = RedisCacheUtils.get(keyPrefix+"MlClass");
        if (StrUtil.isNotBlank(mlClassResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlClassResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs =metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds=new ArrayList<>();
            permanentAttrs.forEach(item->{
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql="SELECT "+StrUtil.join(",",fileds)+" FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'";
            List<Map<String, Object>> mlClassQueryResult = namedParameterJdbcTemplate.queryForList(sql, new MapSqlParameterSource());
            mlClassQueryResult.forEach(map -> map.put("itable_name", "ml_class"));
            combinedQueryResult.addAll(mlClassQueryResult);
        }
        String clsAttrResult = RedisCacheUtils.get(keyPrefix+"ClsAttr");
        if (StrUtil.isNotBlank(clsAttrResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(clsAttrResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs =metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds=new ArrayList<>();
            permanentAttrs.forEach(item->{
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql="SELECT "+StrUtil.join(",",fileds)+" FROM cls_attr WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'";

            List<Map<String, Object>> clsAttrQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            clsAttrQueryResult.forEach(map -> map.put("itable_name", "cls_attr"));
            combinedQueryResult.addAll(clsAttrQueryResult);
        }
        String clsMsgResult = RedisCacheUtils.get(keyPrefix+"ClsMsg");
        if (StrUtil.isNotBlank(clsMsgResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(clsMsgResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs =metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds=new ArrayList<>();
            permanentAttrs.forEach(item->{
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql="SELECT "+StrUtil.join(",",fileds)+" FROM cls_msg WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'";

            List<Map<String, Object>> clsMsgQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            clsMsgQueryResult.forEach(map -> map.put("itable_name", "cls_msg"));
            combinedQueryResult.addAll(clsMsgQueryResult);
        }
        String ClsConstResult = RedisCacheUtils.get(keyPrefix+"ClsConst");
        if (StrUtil.isNotBlank(ClsConstResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(ClsConstResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs =metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds=new ArrayList<>();
            permanentAttrs.forEach(item->{
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql="SELECT "+StrUtil.join(",",fileds)+" FROM cls_const WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');";

            List<Map<String, Object>> clsConstQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            clsConstQueryResult.forEach(map -> map.put("itable_name", "cls_const"));
            combinedQueryResult.addAll(clsConstQueryResult);
        }
        String MlAttributeResult = RedisCacheUtils.get(keyPrefix+"MlAttribute");
        if (StrUtil.isNotBlank(MlAttributeResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(MlAttributeResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs =metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds=new ArrayList<>();
            permanentAttrs.forEach(item->{
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql="SELECT "+StrUtil.join(",",fileds)+" FROM ml_attribute WHERE obj_id IN(SELECT right_id FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> mlAttributeQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlAttributeQueryResult.forEach(map -> map.put("itable_name", "ml_attribute"));
            combinedQueryResult.addAll(mlAttributeQueryResult);
        }
        String mlMessageResult = RedisCacheUtils.get(keyPrefix+"MlMessage");
        if (StrUtil.isNotBlank(mlMessageResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlMessageResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs =metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds=new ArrayList<>();
            permanentAttrs.forEach(item->{
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql="SELECT "+StrUtil.join(",",fileds)+" FROM ml_message WHERE obj_id IN(SELECT right_id FROM cls_msg WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> mlMessageQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlMessageQueryResult.forEach(map -> map.put("itable_name", "ml_message"));
            combinedQueryResult.addAll(mlMessageQueryResult);
        }
        String mlConstantResult = RedisCacheUtils.get(keyPrefix+"MlConstant");
        if (StrUtil.isNotBlank(mlConstantResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlConstantResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM ml_constant WHERE obj_id IN(SELECT right_id FROM cls_const WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> mlConstantQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlConstantQueryResult.forEach(map -> map.put("itable_name", "ml_constant"));
            combinedQueryResult.addAll(mlConstantQueryResult);
        }
        String mlRuleResult = RedisCacheUtils.get(keyPrefix+"MlRule");
        if (StrUtil.isNotBlank(mlRuleResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlRuleResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM ml_rule WHERE code IN(SELECT rule_code FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0') and rule_code !='' and rule_code is not null);";

            List<Map<String, Object>> mlRuleQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlRuleQueryResult.forEach(map -> map.put("itable_name", "ml_rule"));
            combinedQueryResult.addAll(mlRuleQueryResult);
        }
        // 智能体厂
        // List<Map<String, Object>> mlDbInfoQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM " +
        //          "ml_db_info WHERE obj_id IN(SELECT right_id FROM svr_db WHERE left_id IN(SELECT obj_id FROM " +
        //           "ml_service WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));",
        //        new MapSqlParameterSource());
        // List<Map<String, Object>> svrDbQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM svr_db " +
        //        "WHERE left_id IN(SELECT obj_id FROM ml_service WHERE (container in('LeVault','BPM','EntArch',
        //        'DME') or code='bizsvr') and tenant_id='0');", new MapSqlParameterSource());
        String mlLangResult = RedisCacheUtils.get(keyPrefix+"MlLang");
        if (StrUtil.isNotBlank(mlLangResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlLangResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM ml_lang WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0';";

            List<Map<String, Object>> mlLangQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlLangQueryResult.forEach(map -> map.put("itable_name", "ml_lang"));
            combinedQueryResult.addAll(mlLangQueryResult);
        }
//        List<Map<String, Object>> mlCodeRepoQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM ml_code_repo", new MapSqlParameterSource());
        String attrLovResult = RedisCacheUtils.get(keyPrefix+"AttrLov");
        if (StrUtil.isNotBlank(attrLovResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(attrLovResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM attr_lov WHERE left_id in(SELECT obj_id FROM ml_attribute WHERE obj_id IN(SELECT right_id FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0')));";

            List<Map<String, Object>> attrLovQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            attrLovQueryResult.forEach(map -> map.put("itable_name", "attr_lov"));
            combinedQueryResult.addAll(attrLovQueryResult);
        }
        String attrCodeResult = RedisCacheUtils.get(keyPrefix+"AttrCode");
        if (StrUtil.isNotBlank(attrCodeResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(attrCodeResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM attr_code WHERE left_id in(SELECT obj_id FROM" +
                    " ml_attribute WHERE obj_id IN(SELECT right_id FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0')));";

            List<Map<String, Object>> attrCodeQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            attrCodeQueryResult.forEach(map -> map.put("itable_name", "attr_code"));
            combinedQueryResult.addAll(attrCodeQueryResult);
        }
        String mlDialogLayoutResult = RedisCacheUtils.get(keyPrefix+"MlDialogLayout");
        if (StrUtil.isNotBlank(mlDialogLayoutResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlDialogLayoutResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM ml_dialog_layout where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0';";

            List<Map<String, Object>> mlDialogLayOutQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlDialogLayOutQueryResult.forEach(map -> map.put("itable_name", "ml_dialog_layout"));
            combinedQueryResult.addAll(mlDialogLayOutQueryResult);
        }
        String mlIndexResult = RedisCacheUtils.get(keyPrefix+"MlIndex");
        if (StrUtil.isNotBlank(mlIndexResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlIndexResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + "  FROM ml_index where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0';";

            List<Map<String, Object>> mlIndexQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlIndexQueryResult.forEach(map -> map.put("itable_name", "ml_index"));
            combinedQueryResult.addAll(mlIndexQueryResult);
        }
        String mlAttrRelResult = RedisCacheUtils.get(keyPrefix+"MlAttrRel");
        if (StrUtil.isNotBlank(mlAttrRelResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlAttrRelResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + "  FROM ml_attr_rel where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0';";

            List<Map<String, Object>> mlAttrRelQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlAttrRelQueryResult.forEach(map -> map.put("itable_name", "ml_attr_rel"));
            combinedQueryResult.addAll(mlAttrRelQueryResult);
        }
        String mlRuntimeClsResult = RedisCacheUtils.get(keyPrefix+"MlRuntimeCls");
        if (StrUtil.isNotBlank(mlRuntimeClsResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlRuntimeClsResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + "  FROM ml_runtime_cls where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0';";

            List<Map<String, Object>> mlRuntimeClassQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlRuntimeClassQueryResult.forEach(map -> map.put("itable_name", "ml_runtime_cls"));
            combinedQueryResult.addAll(mlRuntimeClassQueryResult);
        }
        List<Map<String, Object>> mlServiceIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_service WHERE (container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') or code='bizsvr') and tenant_id='0');", new MapSqlParameterSource());
        List<Map<String, Object>> mlClassIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');", new MapSqlParameterSource());
        List<Map<String, Object>> clsAttrIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM cls_attr WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');", new MapSqlParameterSource());
        List<Map<String, Object>> clsAttrRelationIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * " +
                "FROM " +
                "relation_id where obj_id in(SELECT obj_id FROM cls_attr WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');", new MapSqlParameterSource());

        List<Map<String, Object>> clsMsgIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM cls_msg WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');", new MapSqlParameterSource());
        List<Map<String, Object>> clsMsgRelationIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * " +
                "FROM " +
                "relation_id where obj_id in(SELECT obj_id FROM cls_msg WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');", new MapSqlParameterSource());

        List<Map<String, Object>> clsConstIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM cls_const WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));", new MapSqlParameterSource());
        List<Map<String, Object>> clsConstRelationIdIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM relation_id where obj_id in(SELECT obj_id FROM cls_const WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));", new MapSqlParameterSource());

        List<Map<String, Object>> mlAttributeIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_attribute WHERE obj_id IN(SELECT right_id FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0')));", new MapSqlParameterSource());
        List<Map<String, Object>> mlMessageIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_message WHERE obj_id IN(SELECT right_id FROM cls_msg WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0')));", new MapSqlParameterSource());
        List<Map<String, Object>> mlConstantIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_constant WHERE obj_id IN(SELECT right_id FROM cls_const WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0')));", new MapSqlParameterSource());
        List<Map<String, Object>> mlRuleIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_rule WHERE code IN(SELECT rule_code FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0') and rule_code !='' and rule_code is not null));", new MapSqlParameterSource());
        List<Map<String, Object>> mlDbInfoIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_db_info WHERE obj_id IN(SELECT right_id FROM svr_db WHERE left_id IN(SELECT obj_id FROM ml_service WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0')));", new MapSqlParameterSource());
        List<Map<String, Object>> svrDbIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM svr_db WHERE left_id IN(SELECT obj_id FROM ml_service WHERE (container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') or code='bizsvr') and tenant_id='0'));", new MapSqlParameterSource());
        List<Map<String, Object>> svrDbRelationIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM" +
                " relation_id where obj_id in(SELECT obj_id FROM svr_db WHERE left_id IN(SELECT obj_id FROM ml_service WHERE (container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') or code='bizsvr') and tenant_id='0'));", new MapSqlParameterSource());

        List<Map<String, Object>> mlLangIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_lang WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');", new MapSqlParameterSource());
//        List<Map<String, Object>> mlCodeRepoIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_code_repo);", new MapSqlParameterSource());
        List<Map<String, Object>>  attrLovIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM attr_lov WHERE left_id in(SELECT obj_id FROM ml_attribute WHERE obj_id IN(SELECT right_id FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'))));", new MapSqlParameterSource());
        List<Map<String, Object>>  attrCodeIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM attr_code WHERE left_id in(SELECT obj_id FROM ml_attribute WHERE obj_id IN(SELECT right_id FROM cls_attr WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'))));", new MapSqlParameterSource());
        List<Map<String, Object>>  mlDialogLayOutIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM ml_dialog_layout where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0');", new MapSqlParameterSource());
        List<Map<String, Object>>  mlIndexIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM " +
                "unique_id where obj_id in(SELECT obj_id FROM ml_index where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') " +
                "and tenant_id='0');", new MapSqlParameterSource());
        List<Map<String, Object>>  mlAttRelIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM " +
                "unique_id where obj_id in(SELECT obj_id FROM ml_attr_rel where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') " +
                "and tenant_id='0');", new MapSqlParameterSource());
        List<Map<String, Object>>  mlRuntimeClassIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * " +
                "FROM " +
                "unique_id where obj_id in(SELECT obj_id FROM ml_runtime_cls where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') " +
                "and tenant_id='0');", new MapSqlParameterSource());

        // 合并两个查询的结果

        //   mlDbInfoQueryResult.forEach(map -> map.put("itable_name", "ml_db_info"));
        //    svrDbQueryResult.forEach(map -> map.put("itable_name", "svr_db"));

        //  mlCodeRepoQueryResult.forEach(map -> map.put("itable_name", "ml_code_repo"));







        mlServiceIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlClassIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        clsAttrIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        clsMsgIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        clsConstIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlAttributeIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlMessageIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlConstantIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlRuleIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlDbInfoIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        svrDbIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));

        mlLangIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        // mlCodeRepoIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        attrLovIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        attrCodeIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlDialogLayOutIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlIndexIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlAttRelIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        mlRuntimeClassIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        svrDbRelationIdQueryResult.forEach(map -> map.put("itable_name", "relation_id"));
        clsAttrRelationIdQueryResult.forEach(map -> map.put("itable_name", "relation_id"));
        clsMsgRelationIdQueryResult.forEach(map -> map.put("itable_name", "relation_id"));
        clsConstRelationIdIdQueryResult.forEach(map -> map.put("itable_name", "relation_id"));
        // combinedQueryResult.addAll(mlDbInfoQueryResult);
        // combinedQueryResult.addAll(svrDbQueryResult);
        combinedQueryResult.addAll(mlServiceIdQueryResult);
        combinedQueryResult.addAll(mlClassIdQueryResult);
        combinedQueryResult.addAll(clsAttrIdQueryResult);
        combinedQueryResult.addAll(clsMsgIdQueryResult);
        combinedQueryResult.addAll(clsConstIdQueryResult);
        combinedQueryResult.addAll(mlAttributeIdQueryResult);
        combinedQueryResult.addAll(mlMessageIdQueryResult);
        combinedQueryResult.addAll(mlConstantIdQueryResult);
        combinedQueryResult.addAll(mlRuleIdQueryResult);
        //combinedQueryResult.addAll(mlDbInfoIdQueryResult);
        //combinedQueryResult.addAll(svrDbIdQueryResult);
        combinedQueryResult.addAll(mlLangIdQueryResult);
        // combinedQueryResult.addAll(mlCodeRepoIdQueryResult);
        combinedQueryResult.addAll(attrLovIdQueryResult);
        combinedQueryResult.addAll(attrCodeIdQueryResult);
        combinedQueryResult.addAll(mlDialogLayOutIdQueryResult);
        combinedQueryResult.addAll(mlIndexIdQueryResult);
        combinedQueryResult.addAll(mlAttRelIdQueryResult);
        combinedQueryResult.addAll(mlRuntimeClassIdQueryResult);
        combinedQueryResult.addAll(svrDbRelationIdQueryResult);
        combinedQueryResult.addAll(clsAttrRelationIdQueryResult);
        combinedQueryResult.addAll(clsMsgRelationIdQueryResult);
        combinedQueryResult.addAll(clsConstRelationIdIdQueryResult);
        // 生成动态列的SQL脚本并导出为文件
        exportSqlScript(combinedQueryResult, "2智能体厂","智能体厂");

        //模型解析
        List<Map<String, Object>> metaCombinedQueryResult = new ArrayList<>();
        String metaServiceResult = RedisCacheUtils.get(keyPrefix+"MetaService");
        if (StrUtil.isNotBlank(metaServiceResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaServiceResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_service where code in (select code from ml_service where (container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') or code='bizsvr') and tenant_id='0');";

            List<Map<String, Object>> metaServiceQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaServiceQueryResult.forEach(map -> map.put("itable_name", "meta_service"));
            metaCombinedQueryResult.addAll(metaServiceQueryResult);
        }
        String metaClassResult = RedisCacheUtils.get(keyPrefix+"MetaClass");
        if (StrUtil.isNotBlank(metaClassResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaClassResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_class where service_code in (select code from meta_service where code in (select code from ml_service where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> metaClassQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaClassQueryResult.forEach(map -> map.put("itable_name", "meta_class"));
            metaCombinedQueryResult.addAll(metaClassQueryResult);
        }
        String metaClassConstantResult = RedisCacheUtils.get(keyPrefix+"MetaClassConstant");
        if (StrUtil.isNotBlank(metaClassConstantResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaClassConstantResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_class_constant where service_code in (select code from meta_service where code in (select code from ml_service where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> metaClassConstQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaClassConstQueryResult.forEach(map -> map.put("itable_name", "meta_class_constant"));
            metaCombinedQueryResult.addAll(metaClassConstQueryResult);
        }
        String metaClassAttributeResult = RedisCacheUtils.get(keyPrefix+"MetaClassAttribute");
        if (StrUtil.isNotBlank(metaClassAttributeResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaClassAttributeResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_class_attribute where service_code in (select code from meta_service where code in (select code from ml_service where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> metaClassAttributeQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaClassAttributeQueryResult.forEach(map -> map.put("itable_name", "meta_class_attribute"));
            metaCombinedQueryResult.addAll(metaClassAttributeQueryResult);
        }
        String metaClassMethodResult = RedisCacheUtils.get(keyPrefix+"MetaClassMethod");
        if (StrUtil.isNotBlank(metaClassMethodResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaClassMethodResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_class_method where service_code in (select code from meta_service where code in (select code from ml_service where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> metaClassMethodQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaClassMethodQueryResult.forEach(map -> map.put("itable_name", "meta_class_method"));
            metaCombinedQueryResult.addAll(metaClassMethodQueryResult);
        }
        List<Map<String, Object>> metaDataSourceQueryResult = namedParameterJdbcTemplate.queryForList("select * from meta_data_source where service_code in (select code from meta_service where code in (select code from ml_service where (container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread')or code='bizsvr') and tenant_id='0'));", new MapSqlParameterSource());
        String metaLangDataResult = RedisCacheUtils.get(keyPrefix+"MetaLangData");
        if (StrUtil.isNotBlank(metaLangDataResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaLangDataResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_lang_data where service_code in (select code from meta_service where code in (select code from ml_service where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> metaLangDataQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaLangDataQueryResult.forEach(map -> map.put("itable_name", "meta_lang_data"));
            metaCombinedQueryResult.addAll(metaLangDataQueryResult);
        }
        String metaDialogLayoutResult = RedisCacheUtils.get(keyPrefix+"MetaDialogLayout");
        if (StrUtil.isNotBlank(metaDialogLayoutResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaDialogLayoutResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_dialog_layout where service_code in (select code from meta_service where code in (select code from ml_service where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> metaDialogLayoutQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaDialogLayoutQueryResult.forEach(map -> map.put("itable_name", "meta_dialog_layout"));
            metaCombinedQueryResult.addAll(metaDialogLayoutQueryResult);
        }
        String metaAttrRelResult = RedisCacheUtils.get(keyPrefix+"MetaAttrRel");
        if (StrUtil.isNotBlank(metaAttrRelResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaAttrRelResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_attr_rel where tenant_id=0";

            List<Map<String, Object>> metaAttrRelQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaAttrRelQueryResult.forEach(map -> map.put("itable_name", "meta_attr_rel"));
            metaCombinedQueryResult.addAll(metaAttrRelQueryResult);
        }
        String metaRuntimeClsResult = RedisCacheUtils.get(keyPrefix+"MetaRuntimeCls");
        if (StrUtil.isNotBlank(metaRuntimeClsResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(metaRuntimeClsResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " from meta_runtime_cls where tenant_id=0";

            List<Map<String, Object>> metaRunTimeQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            metaRunTimeQueryResult.forEach(map -> map.put("itable_name", "meta_runtime_cls"));
            metaCombinedQueryResult.addAll(metaRunTimeQueryResult);
        }
        // metaDataSourceQueryResult.forEach(map -> map.put("itable_name", "meta_data_source"));

        //  metaCombinedQueryResult.addAll(metaDataSourceQueryResult);
        // 生成动态列的SQL脚本并导出为文件
        exportSqlScript(metaCombinedQueryResult, "3模型解析","智能体厂");


        List<Map<String, Object>> lovCombinedQueryResult = new ArrayList<>();
        String lovDefResult = RedisCacheUtils.get(keyPrefix+"LovDef");
        if (StrUtil.isNotBlank(lovDefResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(lovDefResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });

            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM lov_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0';";

            List<Map<String, Object>> lovDefQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            lovDefQueryResult.forEach(map -> map.put("itable_name", "lov_def"));
            lovCombinedQueryResult.addAll(lovDefQueryResult);
        }
        String dynLovResult = RedisCacheUtils.get(keyPrefix+"DynLov");
        if (StrUtil.isNotBlank(dynLovResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(dynLovResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add("`" + NancalStringUtils.dumpToUnderline(item) + "`");
            });
            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM dyn_lov where lov_code in (SELECT code FROM lov_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread'));";

            List<Map<String, Object>> dynLovQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            dynLovQueryResult.forEach(map -> map.put("itable_name", "dyn_lov"));
            lovCombinedQueryResult.addAll(dynLovQueryResult);
        }
        String staticLovvResult = RedisCacheUtils.get(keyPrefix+"StaticLov");
        if (StrUtil.isNotBlank(staticLovvResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(staticLovvResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add("`" + NancalStringUtils.dumpToUnderline(item) + "`");
            });
            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM static_lov where `lov_code` in (SELECT code " +
                    "FROM lov_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread'));";

            List<Map<String, Object>> staticLovQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            staticLovQueryResult.forEach(map -> map.put("itable_name", "static_lov"));
            lovCombinedQueryResult.addAll(staticLovQueryResult);
        }
        List<Map<String, Object>> lovDefIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM lov_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread'));", new MapSqlParameterSource());
        List<Map<String, Object>> dynLovIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM dyn_lov where `lov_code` in (SELECT code FROM lov_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread')));", new MapSqlParameterSource());
        List<Map<String, Object>> staticLovIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM static_lov where `lov_code` in (SELECT code FROM lov_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread')));", new MapSqlParameterSource());

        lovDefIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        dynLovIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        staticLovIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        lovCombinedQueryResult.addAll(lovDefIdQueryResult);
        lovCombinedQueryResult.addAll(dynLovIdQueryResult);
        lovCombinedQueryResult.addAll(staticLovIdQueryResult);
        // 生成动态列的SQL脚本并导出为文件
        exportSqlScript(lovCombinedQueryResult, "4lov","智能体厂");


        List<Map<String, Object>> encCodeCombinedQueryResult = new ArrayList<>();
        String codeDefResult = RedisCacheUtils.get(keyPrefix+"CodeDef");
        if (StrUtil.isNotBlank(codeDefResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(codeDefResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add("`" + NancalStringUtils.dumpToUnderline(item) + "`");
            });
            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM code_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread');";

            List<Map<String, Object>> codeDefQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            codeDefQueryResult.forEach(map -> map.put("itable_name", "code_def"));
            encCodeCombinedQueryResult.addAll(codeDefQueryResult);
        }

        String codeSegResult = RedisCacheUtils.get(keyPrefix+"CodeSeg");
        if (StrUtil.isNotBlank(codeSegResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(codeSegResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add("`" + NancalStringUtils.dumpToUnderline(item) + "`");
            });
            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM code_seg where `encode` in (SELECT code FROM code_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread'));";

            List<Map<String, Object>> codeSegQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            codeSegQueryResult.forEach(map -> map.put("itable_name", "code_seg"));
            encCodeCombinedQueryResult.addAll(codeSegQueryResult);
        }
        String codeValResult = RedisCacheUtils.get(keyPrefix+"CodeVal");
        if (StrUtil.isNotBlank(codeValResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(codeValResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs = metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds = new ArrayList<>();
            permanentAttrs.forEach(item -> {
                fileds.add("`" + NancalStringUtils.dumpToUnderline(item) + "`");
            });
            String sql = "SELECT " + StrUtil.join(",", fileds) + " FROM code_val where `encode` in (SELECT code FROM code_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread'));";

            List<Map<String, Object>> codeValQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            codeValQueryResult.forEach(map -> map.put("itable_name", "code_val"));
            encCodeCombinedQueryResult.addAll(codeValQueryResult);
        }
        List<Map<String, Object>> codeDefIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM code_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread'));", new MapSqlParameterSource());
        List<Map<String, Object>> codeSegIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM code_seg where `encode` in (SELECT code FROM code_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread')));", new MapSqlParameterSource());
        List<Map<String, Object>> codeValIdQueryResult = namedParameterJdbcTemplate.queryForList("SELECT * FROM unique_id where obj_id in(SELECT obj_id FROM code_val where `encode` in (SELECT code FROM code_def where container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread')));", new MapSqlParameterSource());






        codeDefIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        codeSegIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));
        codeValIdQueryResult.forEach(map -> map.put("itable_name", "unique_id"));

        encCodeCombinedQueryResult.addAll(codeDefIdQueryResult);
        encCodeCombinedQueryResult.addAll(codeSegIdQueryResult);
        encCodeCombinedQueryResult.addAll(codeValIdQueryResult);
        // 生成动态列的SQL脚本并导出为文件
        exportSqlScript(encCodeCombinedQueryResult, "5编码规则", "智能体厂");
    }
    private void exportSqlScript(List<Map<String, Object>> queryResult, String outputFileName, String fileName) {
        List<String> sqlStatements = generateDynamicReplaceStatements(queryResult);

        // 创建文件夹
        File folder = new File(fileName);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        // 确定文件路径
        String fileName1 = folder.getAbsolutePath() + File.separator +"智能体厂.sql";

        // 使用追加模式写入文件
        try (PrintWriter writer = new PrintWriter(new FileWriter(fileName1, true))) {
            for (String sqlStatement : sqlStatements) {
                writer.println(sqlStatement);
            }
            System.out.println(outputFileName+"SQL脚本已追加到文件：" + fileName1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private List<String> generateDynamicReplaceStatements(List<Map<String, Object>> queryResult) {
        List<String> sqlStatements = new ArrayList<>();

        for (Map<String, Object> result : queryResult) {
            sqlStatements.add(generateDynamicReplaceStatement(result));
        }

        return sqlStatements;
    }

    private String generateDynamicReplaceStatement(Map<String, Object> result) {
        // 根据查询结果动态生成列名和值
        StringBuilder columnsStringBuilder = new StringBuilder();
        StringBuilder valuesStringBuilder = new StringBuilder();
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String tableName = (String) result.get("itable_name");

        for (Map.Entry<String, Object> entry : result.entrySet()) {
            String columnName = entry.getKey();
            Object columnValue = entry.getValue();
            if (columnName.equals("creator")||columnName.equals("modifier")){
                columnValue="1648877357806776320";
            }

            if (!"itable_name".equals(columnName)) {
                columnsStringBuilder.append("`").append(columnName).append("`").append(", ");

                // 处理不同类型的值
                if (columnValue instanceof String) {
                    if (columnName.equals("expression")||columnName.equals("regular_expression")) {
                        valuesStringBuilder.append("'").append(escapeRegex(escapeSqlString((String) columnValue))).append("', ");
                    }else if (columnName.equals("req_example")||columnName.equals("resp_example")) {
                        boolean flag = JSONUtil.isTypeJSON((String) columnValue);
                        if (flag){
                            valuesStringBuilder.append("'").append(columnValue).append("', ");
                        }else {
                            valuesStringBuilder.append("'").append("', ");
                        }
                    }
                    else {
                        valuesStringBuilder.append("'").append(escapeSqlString((String) columnValue)).append("', ");
                    }
                } else if (columnValue instanceof LocalDateTime) {
                    valuesStringBuilder.append("'").append(((LocalDateTime) columnValue).format(dateFormatter)).append("', ");
                }
                else if (columnValue instanceof Date) {
                    valuesStringBuilder.append("'").append(columnValue).append("', ");
                }else {
                    valuesStringBuilder.append(columnValue).append(", ");
                }
            }
        }

        // 移除最后一个逗号和空格
        String columnsString = columnsStringBuilder.substring(0, columnsStringBuilder.length() - 2);
        String valuesString = valuesStringBuilder.substring(0, valuesStringBuilder.length() - 2);

        return String.format("REPLACE INTO `%s` (%s) VALUES (%s);",tableName, columnsString, valuesString);
    }


    private String escapeSqlString(String value) {
        // 处理多行 JSON 字符串，将其转换为单行
        String processedValue = value.replaceAll("\\s+", " ").replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"");
        // 对其他字符串值进行正常的 SQL 转义处理
        // ...

        return processedValue;
    }
    private String escapeRegexMessge(String regex) {
        // 对正则表达式中的反斜杠进行双重转义
        String escapedRegex = regex.replace("\\", "");

        // 对正则表达式中的单引号进行转义
        escapedRegex = escapedRegex.replace("'", "''");

        return escapedRegex;
    }

    private String escapeRegex(String regex) {
        // 对正则表达式中的反斜杠进行双重转义
        String escapedRegex = regex.replace("\\", "\\\\");

        // 对正则表达式中的单引号进行转义
        escapedRegex = escapedRegex.replace("'", "''");

        return escapedRegex;
    }


    @GetMapping("/exportMessageRemark")
    public void exportMessageRemark() throws JsonProcessingException {
        String tenantId="0";
        String keyPrefix= RedisCacheNames.getMetaClassPrefix() + tenantId +":";
        String mlMessageResult = RedisCacheUtils.get(keyPrefix+"MlMessage");
        List<Map<String, Object>> combinedQueryResult = new ArrayList<>();
        if (StrUtil.isNotBlank(mlMessageResult)) {
            ObjectMapper objectMapper = new ObjectMapper();
            MetaClassInfo metaClassInfo = objectMapper.readValue(mlMessageResult, MetaClassInfo.class);
            List<MetaClassAttributeVo> metaAttrs = metaClassInfo.getMetaAttrs();
            List<String> permanentAttrs =metaAttrs.stream().filter(metaClassAttributeVo -> metaClassAttributeVo.getType().equals(AttributeInfoCategoryEnum.PERMANENT)).map(MetaClassAttributeVo::getCode).toList();
            List<String> fileds=new ArrayList<>();
            permanentAttrs.forEach(item->{
                fileds.add(NancalStringUtils.dumpToUnderline(item));
            });
            String sql="SELECT "+StrUtil.join(",",fileds)+" FROM ml_message WHERE obj_id IN(SELECT right_id FROM cls_msg WHERE left_id IN(SELECT obj_id FROM ml_class WHERE container in('LeVault','BPM','EntArch','DME','PKM','SampleData','DigitalThread') and tenant_id='0'));";

            List<Map<String, Object>> mlMessageQueryResult = namedParameterJdbcTemplate.queryForList(sql,
                    new MapSqlParameterSource());
            mlMessageQueryResult.forEach(map -> map.put("itable_name", "ml_message"));
            combinedQueryResult.addAll(mlMessageQueryResult);
        }
        // 生成动态列的SQL脚本并导出为文件
        exportSqlScriptRemark(combinedQueryResult, "消息池","消息池");

    }
    private void exportSqlScriptRemark(List<Map<String, Object>> queryResult, String outputFileName, String fileName) {
        List<String> sqlStatements = generateDynamicReplaceStatementsMessage(queryResult);

        // 创建文件夹
        File folder = new File(fileName);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        // 确定文件路径
        String fileName1 = folder.getAbsolutePath() + File.separator +"更新消息池.sql";

        // 使用追加模式写入文件
        try (PrintWriter writer = new PrintWriter(new FileWriter(fileName1, true))) {
            for (String sqlStatement : sqlStatements) {
                writer.println(sqlStatement);
            }
            System.out.println(outputFileName+"SQL脚本已追加到文件：" + fileName1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private List<String> generateDynamicReplaceStatementsMessage(List<Map<String, Object>> queryResult) {
        List<String> sqlStatements = new ArrayList<>();

        for (Map<String, Object> result : queryResult) {
            sqlStatements.add(generateDynamicReplaceStatementMessage(result));
        }

        return sqlStatements;
    }

    private String generateDynamicReplaceStatementMessage(Map<String, Object> result) {
        // 根据查询结果动态生成列名和值
        StringBuilder setStringBuilder = new StringBuilder();
        StringBuilder whereStringBuilder = new StringBuilder();
        String tableName = (String) result.get("itable_name");

        for (Map.Entry<String, Object> entry : result.entrySet()) {
            String columnName = entry.getKey();
            Object columnValue = entry.getValue();

            // 如果是creator或modifier字段，则将其值设置为指定的ID
            if (columnName.equals("creator") || columnName.equals("modifier")) {
                columnValue = "1648877357806776320"; // 设置为指定值
            }

            if (!"itable_name".equals(columnName)) {
                // 构建SET子句
//                if (columnValue instanceof String) {
//                    if (columnName.equals("expression") || columnName.equals("regular_expression")) {
//                        setStringBuilder.append("`").append(columnName).append("` = '")
//                                .append(escapeRegex(escapeSqlString((String) columnValue))).append("', ");
//                    } else if (columnName.equals("req_example") || columnName.equals("resp_example")) {
//                        boolean flag = JSONUtil.isTypeJSON((String) columnValue);
//                        if (flag) {
//                            setStringBuilder.append("`").append(columnName).append("` = '")
//                                    .append(escapeRegex(escapeSqlString((String) columnValue))).append("', ");
//                        } else {
//                            setStringBuilder.append("`").append(columnName).append("` = '', ");
//                        }
//                    }
//                }
                if (columnName.equals("code")) {
                    whereStringBuilder.append("`code` = '").append(columnValue).append("'");
                }
                if (columnName.equals("remark")|| columnName.equals("req_body")|| columnName.equals("resp_body")){
                    setStringBuilder.append("`").append(columnName).append("` = '")
                             .append( columnValue).append("', ");
                }
                if (columnName.equals("req_example") || columnName.equals("resp_example")){
                    boolean flag = JSONUtil.isTypeJSON((String) columnValue);
                    if (flag) {
                           setStringBuilder.append("`").append(columnName).append("` = '")
                                   .append(escapeRegexMessge(escapeSqlString((String) columnValue))).append("', ");
                        } else {
                           setStringBuilder.append("`").append(columnName).append("` = '', ");
                       }
                    }
                }

        }

// 假设“itable_name”是主键或唯一标识列，生成WHERE子句


// 移除SET子句和WHERE子句中的最后一个逗号和空格
        String setString = setStringBuilder.substring(0, setStringBuilder.length() - 2);
        String whereString = whereStringBuilder.toString();

// 返回UPDATE语句
        return String.format("UPDATE `%s` SET %s WHERE %s;", tableName, setString, whereString);
    }


}
